﻿using System.Web.UI.WebControls;
using System.Data;
using System.Collections.Generic;
using System;

namespace ART.Common
{
    /// <summary>
    /// 页面控件操作类
    /// Create by Jinlong Zhang
    /// </summary>
    public class JBind : System.Web.UI.Page
    {


        #region __________Repeater__________



        /// <summary>
        /// 绑顶数据源到指定的Repeater
        /// </summary>
        /// <param name="repList">DataList控件</param>
        /// <param name="dataSource">数据源</param>
        public static void BindList(System.Web.UI.WebControls.Repeater repList, DataTable dataSource)
        {
            repList.DataSource = dataSource;
            repList.DataBind();
        }


        #endregion



        #region __________DataList__________
        /// <summary>
        /// 绑顶数据源到指定的DataList
        /// </summary>
        /// <param name="dlList">DataList控件</param>
        /// <param name="dataSource">数据源</param>
        public static void BindList(System.Web.UI.WebControls.DataList dlList, DataTable dataSource)
        {
            dlList.DataSource = dataSource;
            dlList.DataBind();
        }
        #endregion



        #region __________CheckBoxList__________

        public static void BindList(System.Web.UI.WebControls.CheckBoxList chklst, string[] ArrValue)
        {
            chklst.Items.Clear();
            for (int i = 0; i < ArrValue.Length; i++)
            {
                if (ArrValue[i].ToString().Trim().Length > 0)
                {
                    ListItem item = new ListItem();
                    item.Text = ArrValue[i].ToString();
                    item.Value = ArrValue[i].ToString();
                    chklst.Items.Add(item);
                }
            }

        }

        /// <summary>
        /// 绑顶数据源到指定的CheckBoxList
        /// </summary>
        /// <param name="chklst">CheckBoxList控件</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="textField">文本字段</param>
        /// <param name="valueField">值字段</param>
        public static void BindList(System.Web.UI.WebControls.CheckBoxList chklst, DataTable dataSource, string textField, string valueField)
        {
            chklst.DataSource = dataSource;
            chklst.DataTextField = textField;
            chklst.DataValueField = valueField;
            chklst.DataBind();
        }


        /// <summary>
        /// 使CheckBoxList中指定集合的项选中
        /// </summary>
        /// <param name="chklst">CheckBoxList控件</param>
        /// <param name="itemValues">要匹配的值的集合</param>
        public static void SetSelectedList(System.Web.UI.WebControls.CheckBoxList chklst, string[] itemValues)
        {
            chklst.SelectedIndex = -1;
            foreach (string item in itemValues)
            {
                if (item != null)
                {
                    foreach (ListItem it in chklst.Items)
                    {
                        if (it.Value == item)
                        {
                            it.Selected = true;
                        }
                    }
                }
            }
        }


        /// <summary>
        /// 返回选中CheckBoxList的string[]数组
        /// </summary>
        /// <param name="chklst">CheckBoxList控件</param>

        public static string GetSelectedList(System.Web.UI.WebControls.CheckBoxList chklst)
        {
            if (chklst.Items.Count == 0)
                return null;
            string items = "";
            foreach (ListItem item in chklst.Items)
            {
                if (item.Selected)
                {
                    items += "," + item.Value;
                }
            }
            if (items == string.Empty)
                return null;
            items = items.Substring(1);
            return items;
        }



        #endregion



        #region __________RadioButtonList__________



        /// <summary>
        /// 绑顶数据源到指定的RadioButtonList
        /// </summary>
        /// <param name="radlst">CheckBoxList控件</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="textField">文本字段</param>
        /// <param name="valueField">值字段</param>
        public static void BindList(System.Web.UI.WebControls.RadioButtonList radlst, DataTable dataSource, string textField, string valueField)
        {
            radlst.DataSource = dataSource;
            radlst.DataTextField = textField;
            radlst.DataValueField = valueField;
            radlst.DataBind();
        }


        /// <summary>
        /// 使RadioButtonList中指定值的项选中
        /// </summary>
        /// <param name="radlst">RadioButtonList控件</param>
        /// <param name="itemValue">要匹配的值的集合</param>
 
        public static void SetSelectedList(System.Web.UI.WebControls.RadioButtonList radlst, string itemValue)
        {
            radlst.SelectedIndex = -1;
            foreach (ListItem it in radlst.Items)
            {
                if (it.Value == itemValue)
                {
                    it.Selected = true;
                    return;
                }
            }
        }



        /// <summary>
        /// 返回选中RadioButtonList的string
        /// </summary>
        /// <param name="radlst">RadioButtonList控件</param>
 
        public static string GetSelectedList(System.Web.UI.WebControls.RadioButtonList radlst)
        {
            if (radlst.Items.Count == 0)
                return "";
            foreach (ListItem item in radlst.Items)
            {
                if (item.Selected)
                {
                    return item.Value;
                }
            }
            return "";
        }


        #endregion



        #region __________ListBox__________



        /// <summary>
        /// 绑顶数据源到指定的ListBox
        /// </summary>
        /// <param name="lstbox">ListBox控件</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="textField">文本字段</param>
        /// <param name="valueField">值字段</param>
        public static void BindList(System.Web.UI.WebControls.ListBox lstbox, DataTable dataSource, string textField, string valueField)
        {
            lstbox.DataSource = dataSource;
            lstbox.DataTextField = textField;
            lstbox.DataValueField = valueField;
            lstbox.DataBind();
        }


        /// <summary>
        /// 使ListBox中指定集合的项选中
        /// </summary>
        /// <param name="lstbox">ListBox控件</param>
        /// <param name="itemValues">要匹配的值的集合</param>
 
        public static void SetSelectedList(System.Web.UI.WebControls.ListBox lstbox, string[] itemValues)
        {
            lstbox.SelectedIndex = -1;
            foreach (string item in itemValues)
            {
                if (item != null)
                {
                    foreach (ListItem it in lstbox.Items)
                    {
                        if (it.Value == item)
                        {
                            it.Selected = true;
                        }
                    }
                }
            }
        }



        /// <summary>
        /// 返回选中ListBox的string[]数组
        /// </summary>
        /// <param name="lstbox">ListBox控件</param>
 
        public static string GetSelectedList(System.Web.UI.WebControls.ListBox lstbox)
        {
            if (lstbox.Items.Count == 0)
                return null;
            string items = "";
            foreach (ListItem item in lstbox.Items)
            {
                if (item.Selected)
                {
                    items += "," + item.Value;
                }
            }
            if (items == string.Empty)
                return null;
            items = items.Substring(1);
            return items;
        }


        #endregion



        #region __________DropDownList__________


        /// <summary>
        /// 绑顶数据源到指定的DropDownList
        /// </summary>
        /// <param name="drp">DropDownList控件</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="textField">文本字段</param>
        /// <param name="valueField">值字段</param>
        public static void BindList(System.Web.UI.WebControls.DropDownList drp, DataTable dataSource, string textField, string valueField)
        {
            drp.Items.Clear();
            foreach (DataRow dr in dataSource.Rows)
            {
                drp.Items.Add(new ListItem(dr[textField].ToString(), dr[valueField].ToString()));
            }
        }


        /// <summary>
        /// 绑顶数据源到指定的DropDownList
        /// </summary>
        /// <param name="drp">DropDownList控件</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="textField">文本字段</param>
        /// <param name="valueField">值字段</param>
        public static void BindList(System.Web.UI.WebControls.DropDownList drp, DataRow[] drs, string textField, string valueField)
        {
            drp.Items.Clear();
            foreach (DataRow dr in drs)
            {
                drp.Items.Add(new ListItem(dr[textField].ToString(), dr[valueField].ToString()));
            }
        }




        /// <summary>
        /// 绑定数据源到指定的DropDownList
        /// </summary>
        public static void BindList(System.Web.UI.WebControls.DropDownList drp, List<ListItem> List)
        {
            drp.Items.Clear();
            foreach (ListItem list in List)
            {
                drp.Items.Add(list);
            }
        }







        /// <summary>
        /// 使DropDownList中指定值的项选中
        /// </summary>
        public static void SetSelectedList(System.Web.UI.WebControls.DropDownList drp, string itemValue)
        {
            drp.SelectedIndex = -1;
            foreach (ListItem item in drp.Items)
            {
                if (item.Value == itemValue)
                {
                    item.Selected = true;
                    return;
                }
            }
        }


        /// <summary>
        /// 返回选中DropDownList的string
        /// </summary>
        /// <param name="drp">DropDownList控件</param>
        /// <param name="textValue">返回Value</param>
        public static string GetSelectedList(System.Web.UI.WebControls.DropDownList drp)
        {
            if (drp.Items.Count == 0)
                return "";
            foreach (ListItem item in drp.Items)
            {
                if (item.Selected)
                {
                    return item.Value;
                }
            }
            return "";
        }


        #endregion




        ///<summary>
        /// 年
        ///</summary>
        public static void BindYear(System.Web.UI.WebControls.DropDownList drp)
        {
            List<ListItem> list = new List<ListItem>();
            int dt_now = DateTime.Now.Year;
            for (int i = dt_now; i >= 1960; i--)
            {
                list.Add(new ListItem(i.ToString(), i.ToString()));
            }
            BindList(drp, list);
        }

        ///<summary>
        /// 月
        ///</summary>
        public static void BindMonth(System.Web.UI.WebControls.DropDownList drp)
        {
            List<ListItem> list = new List<ListItem>();
            for (int i = 1; i <= 12; i++)
            {
                list.Add(new ListItem(i.ToString(), i.ToString()));
            }
            BindList(drp, list);
        }


        ///<summary>
        /// 日
        ///</summary>
        public static void BindDay(System.Web.UI.WebControls.DropDownList drp)
        {
            List<ListItem> list = new List<ListItem>();
            for (int i = 1; i <= 31; i++)
            {
                list.Add(new ListItem(i.ToString(), i.ToString()));
            }
            BindList(drp, list);
        }





    }
}
